import { createSlice } from '@reduxjs/toolkit'
import {
    Toast
} from 'antd-mobile'

export const PersonSlice = createSlice({
    name: 'PersonSlice',
    initialState: {
        PersonList: [
            {
                id: 1,
                name: '崔胜澈',
                code: '141124200502170101',
                state: false,
            },
            {
                id: 2,
                name: '迪丽热巴',
                code: '168845200688741025',
                state: false,
            },
            {
                id: 3,
                name: '喜羊羊',
                code: '197584355985456652',
                state: false,
            },
            {
                id: 4,
                name: '懒羊羊',
                code: '154855699741521265',
                state: false,
            },
        ]
    },
    reducers: {
        SwitchPer(state, { payload }) {
            const item = state.PersonList.find(item => item.id === payload.id);
            item.state = !item.state;
        },
        delPer(state, { payload }) {
            state.PersonList = state.PersonList.filter(
                item => item.id !== payload.id
            );
        },
        addPer(state, { payload }) {
            const item = state.PersonList.find(item => item.id === payload.id);
            if (!item) {
                state.PersonList.push(payload)
                Toast.show('添加成功')
            } else {
                Toast.show('添加失败')
            }
        },
        editPer(state, { payload }) {
            const item = state.PersonList.find(item => item.id === payload.id);
            if (item) {
                Toast.show('编辑成功')
                Object.assign(item, payload)
            } else {
                Toast.show('编辑失败')
            }
        },
    },
})

export const { SwitchPer, delPer, addPer, editPer } = PersonSlice.actions

export default PersonSlice.reducer